package com.eclink.asset.config;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;

import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.List;

@Configuration
public class HttpMessageConverterConfig {
    // 自定义Spring容器中HttpMessageConverters对象，并行相关配置
    @Bean
    public HttpMessageConverters fastJsonHttpMessageConverter() {
        FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
        // 设置FastJsonHttpMessageConverter支持的MediaType
        converter.setSupportedMediaTypes(Arrays.asList(MediaType.APPLICATION_JSON));
        FastJsonConfig config = new FastJsonConfig();
        // 配置统一日期格式化格式
        config.setDateFormat("yyyy-MM-dd HH:mm:ss");
        config.setCharset(Charset.forName("UTF-8"));
        config.setSerializerFeatures(
                SerializerFeature.WriteMapNullValue // 配置返回值中null值时，不允许删除key
        );
        converter.setFastJsonConfig(config);

        // 将FastJsonHttpMessageConverter添加到HttpMessageConverters中
        HttpMessageConverters messageConverters = new HttpMessageConverters(converter);
        List<HttpMessageConverter<?>> converters = messageConverters.getConverters();
        System.out.println("包含的消息转换器 => " + converters);
        return messageConverters;
    }
}
